package admin;

import enums.DeletedStatus;
import models.base.BaseModel;
import param.admin.AdminRoleRequest;
import play.Logger;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static helper.BeanHelper.safeSetValue;

/**
 *  角色
 */
@Entity
@Table(name = "admin_roles")
public class AdminRole extends BaseModel {

    /**
     * 角色名.
     */
    @Column(name = "name", length = 50)
    public String name;

    /**
     * 备注.
     */
    @Column(name = "remark", length = 500)
    public String remark;

    /**
     * 显示顺序.
     */
    @Column(name = "show_order", columnDefinition = "integer")
    public Integer showOrder = 0;


    /**
     * 删除标志.
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "deleted")
    public DeletedStatus deleted;


    //新建
    public static AdminRole create(AdminRoleRequest roleRequest , AdminUser loginUser){

        AdminRole role = new AdminRole();
        safeSetValue(roleRequest.name, value -> role.name = value );
        safeSetValue(roleRequest.showOrder, value -> role.showOrder = value );
        safeSetValue(roleRequest.remark, value -> role.remark = value );

        role.deleted = DeletedStatus.UN_DELETED ;
        role.createAt = new Date() ;
        role.createUser = loginUser ;
        role.save();
        AdminRoleOperate.create(role,roleRequest.businessList);
        return role ;
    }

    //修改
    public  static AdminRole update(AdminRole role , AdminRoleRequest roleRequest , AdminUser loginUser){

        safeSetValue(roleRequest.name, value -> role.name = value );
        safeSetValue(roleRequest.showOrder, value -> role.showOrder = value );
        safeSetValue(roleRequest.remark, value -> role.remark = value );
        role.modifyAt = new Date();
        role.modifyUser = loginUser;
        role.save() ;
        AdminRoleOperate.create(role,roleRequest.businessList);
        return role ;

    }

    public static JPAExtPaginator<AdminRole> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder(" t.deleted = models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.name like {key} ~/")
                .append("/~ and t.corp.id = {corpId} ~/")
                .append("/~ and t.id = {id} ~/");
        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator rolePage = new JPAExtPaginator("Role t" , "t" , AdminRole.class , result.getXsql() , conditionMap).orderBy(orderByExpress);

        Logger.info("-----------------------result :%s" ,result.getXsql() );
        rolePage.setPageNumber(pageNumber);
        rolePage.setPageSize(pageSize);
        rolePage.setBoundaryControlsEnabled(false);
        return rolePage ;

    }

    public static AdminRole findByRoleId( String id) {
        return find("id=?1 and deleted=?2", id, DeletedStatus.UN_DELETED).first();
    }

    public static List<AdminRole> findByCorp() {
        return find(" deleted=?1 order by showOrder",  DeletedStatus.UN_DELETED).fetch();
    }




}
